{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6ad831b3-2025-4789-a8bc-b27dcd531fa5",
   "metadata": {},
   "source": [
    "# Multi-Control-X\n",
    "\n",
    "The multi-control-X applies X gate to one target qubit bit only if the logical AND of all control qubits is satisfied.\n",
    "The multi-control-X function incorporates numerous implementations for the multi-control-X gate,\n",
    "each with a different depth and number of auxiliary qubits.\n",
    "These implementations generically outperform the Gray-code, V-chain and recursive implementations of Ref. [[1]](#1),\n",
    "as well as the relative-phase Toffoli implementation of Ref. [[2]](#2).\n",
    "Given a sufficient number of auxiliary qubits, some implementations allow for logarithmic depth and linear CX-count.\n",
    "The synthesis process selects the appropriate implementation depending on the defined constraints."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5c849116-e74a-4249-9c54-8f163e3d7a55",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "Operator: `control`\n",
    "\n",
    "Arguments:\n",
    "\n",
    "- `ctrl: Union[QBit, QArray[QBit]]`\n",
    "- `operand: QCallable`\n",
    "  \n",
    "\n",
    "Operator `control` takes a qubit array of length one or more as `ctrl`, and applies the `operand` if all qubits are in the `1` state \n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b3ce5912-c51f-4605-ae2d-02edb109565d",
   "metadata": {},
   "source": [
    "## Example\n",
    "\n",
    "The following example shows how to use the `control` operator to implement a Multi-Control-X where a 7-qubit quantum variable serves as the control"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "9629ac10-0774-4140-a207-f210d3cfe4b3",
   "metadata": {},
   "outputs": [],
   "source": [
    "from classiq import (\n",
    "    Output,\n",
    "    QArray,\n",
    "    QBit,\n",
    "    X,\n",
    "    allocate,\n",
    "    control,\n",
    "    create_model,\n",
    "    prepare_bell_state,\n",
    "    qfunc,\n",
    ")\n",
    "\n",
    "\n",
    "@qfunc\n",
    "def main(cntrl: Output[QArray[QBit]], target: Output[QBit]) -> None:\n",
    "    allocate(7, cntrl)\n",
    "    allocate(1, target)\n",
    "    control(ctrl=cntrl, operand=lambda: X(target))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "58821271-3a15-4001-bbfc-740eaa604791",
   "metadata": {},
   "outputs": [],
   "source": [
    "qmod = create_model(main)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "397278e2-68d5-4677-9f14-b5c3e13a7224",
   "metadata": {},
   "outputs": [],
   "source": [
    "from classiq import synthesize, write_qmod\n",
    "\n",
    "write_qmod(qmod, \"mcx_example\")\n",
    "qprog = synthesize(qmod)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d0df544d-9cba-44ec-9bdd-df8b9f0aaaa4",
   "metadata": {},
   "source": [
    "## References\n",
    "\n",
    "<a name=\"1\">[1]</a> A. Barenco et al, Elementary gates for quantum computation,\n",
    "Phys. Rev. A 52 (1995). https://journals.aps.org/pra/abstract/10.1103/PhysRevA.52.3457\n",
    "\n",
    "<a name=\"2\">[2]</a> D. Maslov, Advantages of using relative-phase Toffoli gates\n",
    "with an application to multiple control Toffoli optimization,\n",
    "Phys. Rev. A 93 (2016). https://journals.aps.org/pra/abstract/10.1103/PhysRevA.93.022311"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "dev_py3.11",
   "language": "python",
   "name": "dev_py3.11"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
